package cn.hn.java.summer.socket.command;


import cn.hn.java.summer.exception.MessageCodeException;
import cn.hn.java.summer.socket.command.codec.*;
import cn.hn.java.summer.socket.config.ICommandProtocol;

public class LoginIdCommandHandler extends AbstractCommandHandler {

	@Override
	public ICmdDataDecoder getDataDecoder() {
		return new LoginIdDataDecoder();
	}

	@Override
	public ICmdDataEncoder getDataEncoder() {
		return new LoginIdDataEncoder();
	}

	@Override
	public Object invokeCmd(ICommandProtocol msg, ICmdDataDecoder decoder, ICmdDataEncoder encoder) throws MessageCodeException {

		String loginId="";
		if(msg.getData()!=null){
			loginId=msg.getData().toString().substring(0, LoginIdTools.LOGINID_LENGTH);
		}

		LoginIdDataDecoder loginIdDataDecoder=(LoginIdDataDecoder)decoder;
		LoginIdDataEncoder loginIdDataEncoder=(LoginIdDataEncoder)encoder;
		//设置loginId
		loginIdDataDecoder.setLoginId(loginId);
		loginIdDataEncoder.setLoginId(loginId);
		//调用命令
		Object rst= super.invokeCmd(msg,decoder,encoder);
		//重新设置生成的loginId
		if(rst!=null && rst instanceof ILoginId){
			loginIdDataDecoder.setLoginId(((ILoginId)rst).getLoginId());
		}
		return rst;
	}

}
